<?php

namespace app\controller;

use think\facade\Db;
use think\facade\Request;
use think\facade\Filesystem;
class YardInfo
{
    public function getYardInfo()
    {
        $yardInfo = Db::table('cdydpt_info')->find();
        
        // 检查查询结果是否为空
        if (!$yardInfo) {
            return json(['error' => 'No yard info found'], 404);
        }
    
        // 将数据转换为 UTF-8 编码
        array_walk_recursive($yardInfo, function(&$item, $key) {
            if (!mb_detect_encoding($item, 'utf-8', true)) {
                $item = utf8_encode($item);
            }
        });
    
        // 将 yard_image 转换为 Base64 编码
        $yardInfo['yard_image'] = 'data:image/jpeg;base64,' . base64_encode($yardInfo['yard_image']);
        return json($yardInfo);
    }
    
    
    public function getYardList1()
    {
        $yardList = Db::table('cdydpt_info')
            ->where('yard_id', '>=', 1)
            ->where('yard_id', '<=', 5)
            ->select();
    
        if (!$yardList) {
            return json(['error' => 'No yard list found'], 404);
        }
    
        foreach ($yardList as &$yard) {
            // 检查 yard_image 是否为空
            if (!empty($yard['yard_image'])) {
                // 将 yard_image 转换为 Base64 编码
                $yard['yard_image'] = 'data:image/jpeg;base64,' . base64_encode($yard['yard_image']);
            } else {
                $yard['yard_image'] = ''; // 如果图片为空，设置为空字符串
            }
        }
    
        // 将数据转换为 UTF-8 编码
        array_walk_recursive($yardList, function(&$item, $key) {
            if (!mb_detect_encoding($item, 'utf-8', true)) {
                $item = utf8_encode($item);
            }
        });
    
        return json($yardList);
    }

    public function getYardList2()
    {
        $page = input('get.page', 1);
        $limit = input('get.limit', 3);
        $campusname = input('get.campusname', '');
        $yard_type = input('get.yard_type', '');
        $offset = ($page - 1) * $limit;
    
        $query = Db::table('cdydpt_info')->limit($offset, $limit);
    
        if (!empty($campusname)) {
            $campusnames = explode(',', $campusname);
            $query->whereIn('campusname', $campusnames);
        }
    
        if (!empty($yard_type)) {
            $yard_types = explode(',', $yard_type);
            $query->where(function($q) use ($yard_types) {
                foreach ($yard_types as $type) {
                    $q->whereOr('yard_type', 'LIKE', "%$type%");
                }
            });
        }
    
        $yardList = $query->select();
    
        if (!$yardList) {
            return json(['error' => 'No yard list found'], 404);
        }
    
        foreach ($yardList as &$yard) {
            // 检查 yard_image 是否为空
            if (!empty($yard['yard_image'])) {
                // 将 yard_image 转换为 Base64 编码
                $yard['yard_image'] = 'data:image/jpeg;base64,' . base64_encode($yard['yard_image']);
            } else {
                $yard['yard_image'] = ''; // 如果图片为空，设置为空字符串
            }
        }
    
        // 将数据转换为 UTF-8 编码
        array_walk_recursive($yardList, function(&$item, $key) {
            if (!mb_detect_encoding($item, 'utf-8', true)) {
                $item = utf8_encode($item);
            }
        });
    
        return json($yardList);
    }
    
    
    public function zmd($limit=4)
    {
        $data=Db::name('cdydpt_zmd')->limit($limit)->select();
        return json($data);
    }

    public function getYardList3()
    {
        $page = input('get.page', 1);
        $limit = input('get.limit', 99);
        $campusname = input('get.campusname', '');
        $yard_type = input('get.yard_type', '');
        $offset = ($page - 1) * $limit;
    
        $query = Db::table('cdydpt_info')->limit($offset, $limit);
    
        if (!empty($campusname)) {
            $campusnames = explode(',', $campusname);
            $query->whereIn('campusname', $campusnames);
        }
    
        if (!empty($yard_type)) {
            $yard_types = explode(',', $yard_type);
            $query->where(function($q) use ($yard_types) {
                foreach ($yard_types as $type) {
                    $q->whereOr('yard_type', 'LIKE', "%$type%");
                }
            });
        }
    
        $yardList = $query->select();
    
        if (!$yardList) {
            return json(['error' => 'No yard list found'], 404);
        }
    
        foreach ($yardList as &$yard) {
            // 检查 yard_image 是否为空
            if (!empty($yard['yard_image'])) {
                // 将 yard_image 转换为 Base64 编码
                $yard['yard_image'] = 'data:image/jpeg;base64,' . base64_encode($yard['yard_image']);
            } else {
                $yard['yard_image'] = ''; // 如果图片为空，设置为空字符串
            }
        }
    
        // 将数据转换为 UTF-8 编码
        array_walk_recursive($yardList, function(&$item, $key) {
            if (!mb_detect_encoding($item, 'utf-8', true)) {
                $item = utf8_encode($item);
            }
        });
    
        return json($yardList);
    }

    public function getOrder()
    {
        session_start(); // 开启会话

        $order = Db::name('cdydpt_reserve')->select();

        return $order;
    }

    
}
